<template>
  <div class="page-style">
    <aside class="btn-box">
      <el-row :gutter="15">
        <el-col :span="2">
          <el-button icon="el-icon-plus" type="primary" size="mini" @click="addNews">新增</el-button>
        </el-col>
      </el-row>
    </aside>
    <el-table
      :data="newsList"
      style="width: 100%;font-size:12px;"
    >
      <el-table-column
        prop="title"
        label="新闻标题"
      />
      <el-table-column
        prop="work_people"
        label="作者"
        width="120"
      />
      <el-table-column
        prop="date"
        label="发布时间"
        width="130"
      />
      <el-table-column
        prop="new_date"
        label="最后修改时间"
        width="130"
      />
      <el-table-column
        label="操作"
        width="180"
      >
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="checkInfo(scope.row)">修改</el-button>
          <del-confirm :title="`是否确认删除标题为“${scope.row.title}”的新闻?`" @onConfirm="delFun(scope.row)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" @pagination="paginationChange" />
  </div>
</template>

<script>
import pagination from '@/components/Pagination'
import { getBrandNewsList, operationBrandNews } from '@/api/brand'
import { deletePicture } from '@/api/main'
import delConfirm from '@/components/DelConfirm'
export default {
  components: {
    pagination,
    delConfirm
  },
  data() {
    return {
      total: 0,
      newsList: [],
      p: 1,
      num: 10
    }
  },
  created() {
    this.getNewsList()
  },
  methods: {
    paginationChange(val) {
    //   console.log(val)
      this.p = val.page
      this.num = val.limit
    },
    getNewsList() {
      const data = {
        brand_id: this.$store.state.user.brand_id,
        p: this.p,
        num: this.num
      }
      getBrandNewsList(data).then(res => {
        console.log(res)
        if (res) {
          this.newsList = res.list
          this.total = Number(res.count)
        }
      })
    },
    addNews() {
      this.$router.push({
        path: 'newsDetails'
      })
    },
    checkInfo(row) {
      this.$router.push({
        path: 'newsDetails',
        query: { id: row.id }
      })
    },
    delFun(row) {
      const data = {
        type: 3,
        id: row.id
      }
      const img = row.news_img.split('/')
      const imgName = img[img.length - 1]
      operationBrandNews(data).then(res => {
        if (res.code === 200) {
          this.$message.success('新闻已删除！')
          this.getNewsList()
          deletePicture({ object: imgName }).then(res => {
            if (res.code === 200) {
            //   console.log(res)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
